// by Xeno

private ["_dir_to_set","_m_name","_marker","_d_pod_pos","_exit_it","_m_text"];


if ((player getVariable "d_aapod_available")== 0) exitWith{localize "MSG_DOM_CANNOTBUILDAA" call XfGlobalChat};
if (d_player_busy_building) exitWith{localize "MSG_DOM_ALREADYBUSY" call XfGlobalChat};
if ((player call XfGetHeight) > 5) exitWith {localize "MSG_DOM_YOUAREKIDDING" call XfGlobalChat};

_d_pod_pos = player GetVariable "d_aapod_pos";
if (count _d_pod_pos > 0) exitWith {localize "MSG_DOM_ALREADYPLACEDAA" call XfGlobalChat};

_d_pod_pos = player modeltoworld [0,2,0];

if (surfaceIsWater [_d_pod_pos select 0, _d_pod_pos select 1]) exitWith {
	localize "MSG_DOM_CANNOTBUILDWATER" call XfGlobalChat;
};
d_player_busy_building = true;

_helper1 = "HeliHEmpty" createVehicleLocal [_d_pod_pos select 0, (_d_pod_pos select 1) + 4, 0];
_helper2 = "HeliHEmpty" createVehicleLocal [_d_pod_pos select 0, (_d_pod_pos select 1) - 4, 0];
_helper3 = "HeliHEmpty" createVehicleLocal [(_d_pod_pos select 0) + 4, _d_pod_pos select 1, 0];
_helper4 = "HeliHEmpty" createVehicleLocal [(_d_pod_pos select 0) - 4, _d_pod_pos select 1, 0];

_exit_it = false;
if ((abs (((getPosASL _helper1) select 2) - ((getPosASL _helper2) select 2)) > 2) || (abs (((getPosASL _helper3) select 2) - ((getPosASL _helper4) select 2)) > 2)) then {
	localize "MSG_DOM_PLACENOTVALID" call XfGlobalChat;
	_exit_it = true;
};

for "_mt" from 1 to 4 do {call compile format ["deleteVehicle _helper%1;", _mt]};

if (_exit_it) exitWith {d_player_busy_building = false;};

for "_ic" from 1 to aa_pod_build_duration do {
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 3;
	waitUntil {animationState player != "AinvPknlMstpSlayWrflDnon_medic"};
	if (!(alive player)) exitWith {};
};
d_player_busy_building = false;
if (!(alive player)) exitWith {localize "MSG_DOM_DIEDBEFOREBUILD" call XfGlobalChat};

_dir_to_set = getdir player;

player SetVariable ["d_aapod_pos", _d_pod_pos];

diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"aapod.sqf",d_aa_pod,_d_pod_pos];
_aa_pod = createVehicle [d_aa_pod, _d_pod_pos, [], 0, "NONE"];
_aa_pod setdir _dir_to_set;
[_aa_pod, 0] call XfSetHeight;

player SetVariable ["aa_pod", _aa_pod];
player reveal _aa_pod;

localize "MSG_DOM_AAREADY" call XfGlobalChat;
_m_name = format ["AA Pod %1", str(player)];
_m_text = format ["AA Pod %1", name(player)]; 
["d_p_o_a", [str(player), [_aa_pod,_m_name,name player,playerSide,_m_text]]] call XNetCallEvent;

_aa_pod addAction [localize "MSG_DOM_REMOVEAA" call XRedText, "client\removeaapod.sqf"];
_aa_pod addEventHandler ["killed",{[_this select 0] spawn XAAPodKilled}];

player moveInGunner _aa_pod;